package com.wealth.wechat.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

@SuppressWarnings("all")
public class XmlParser {
	
	/** XML字符串转Document */
	public static Document xmlStr2Dom(String xmlStr){
		try {
			Document xmlDoc = DocumentHelper.parseText(xmlStr);
			return xmlDoc;
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/** XML字符串转Map */
	public static Map<String, Object> xmlStr2Map(String xmlStr){
		Document doc = xmlStr2Dom(xmlStr);
		Map<String, Object> map = Dom2Map(doc);
		return map;
	}

	/** DOM转XML字符串 */
	public static String dom2XmlStr(Document doc){
		return doc.asXML();
	}
	
	/** Map转XML字符串 */
	public static String map2XmlStr(Map<String, Object> map, String rootName){
		Document doc = Map2Dom(map, rootName);
		return dom2XmlStr(doc);
	}
	
	/** Document转Map */
	public static Map<String, Object> Dom2Map(Document doc){
		Map<String, Object> map = new HashMap<String, Object>();
		if(doc == null)
			return map;
		Element root = doc.getRootElement();
		for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
			Element e = (Element) iterator.next();
			List list = e.elements();
			if(list.size() > 0){
				map.put(e.getName(), Dom2Map(e));
			}else
				map.put(e.getName(), e.getText());
		}
		return map;
	}
	
	/** Element转Map */
	public static Map Dom2Map(Element e){
		Map	map = new HashMap();
		List list = e.elements();
		if(list.size() > 0){
			for (int i = 0;i < list.size(); i++) {
				Element iter = (Element) list.get(i);
				List mapList = new ArrayList();
				
				if(iter.elements().size() > 0){
					Map m = Dom2Map(iter);
					if(map.get(iter.getName()) != null){
						Object obj = map.get(iter.getName());
						if(!obj.getClass().getName().equals("java.util.ArrayList")){
							mapList = new ArrayList();
							mapList.add(obj);
							mapList.add(m);
						}
						if(obj.getClass().getName().equals("java.util.ArrayList")){
							mapList = (List) obj;
							mapList.add(m);
						}
						map.put(iter.getName(), mapList);
					}else{
						mapList = new ArrayList();
						mapList.add(m);
						map.put(iter.getName(), mapList);
//						map.put(iter.getName(), m);
					}
				}
				else{
					if(map.get(iter.getName()) != null){
						Object obj = map.get(iter.getName());
						if(!obj.getClass().getName().equals("java.util.ArrayList")){
							mapList = new ArrayList();
							mapList.add(obj);
							mapList.add(iter.getText());
						}
						if(obj.getClass().getName().equals("java.util.ArrayList")){
							mapList = (List) obj;
							mapList.add(iter.getText());
						}
						map.put(iter.getName(), mapList);
					}else
						map.put(iter.getName(), iter.getText());
				}
			}
		}else
			map.put(e.getName(), e.getText());
		return map;
	}
	
	/** Map转换成Document */
	public static Document Map2Dom(Map<String, Object> paramsMap, String rootName){
		if(CommonUtil.isNull(paramsMap))
			return null;
		Document doc = DocumentHelper.createDocument();
		Element rootEle = doc.addElement(rootName);
		for (String key : paramsMap.keySet()) {
			Object obj = paramsMap.get(key);
			Element ele = DocumentHelper.createElement(key);
			if(obj instanceof List){
				List list = (List) obj;
				List<Element> eleList = list2ElementList(list, key);
				rootEle.setContent(eleList);
			}else if(obj instanceof Map){
				Map map = (Map) obj;
				addMap2Element(map, ele);
				rootEle.add(ele);
			}else{
				ele.setText(obj==null?"":obj.toString());
				rootEle.add(ele);
			}
		}
		doc.setRootElement(rootEle);
		return doc;
	}
	
	/** List转Dom元素集List<Element> */
	public static List<Element> list2ElementList(List list, String name){
		List<Element> eleList = new ArrayList<Element>();
		for (Object obj : list) {
			Element ele = DocumentHelper.createElement(name); 
			if(obj instanceof Map){
				Map map = (Map) obj;
				addMap2Element(map, ele);
			}else if(obj instanceof List){
				List list2 = (List) obj;
				List<Element> eleList2 = list2ElementList(list2, name);
				ele.setContent(eleList2);
			}else{
				ele.setText(obj==null?"":obj.toString());
			}
			eleList.add(ele);
		}
		return eleList;
	}

	/** 添加Map到元素中 */
	public static void addMap2Element(Map<String, Object> map, Element ele){
		for (String key : map.keySet()) {
			Element ele2 = DocumentHelper.createElement(key);
			Object obj = map.get(key);
			if(obj instanceof List){
				List list = (List) obj;
				List<Element> eleList = list2ElementList(list, key);
				ele.setContent(eleList);
			}else if(obj instanceof Map){
				Map map2 = (Map) obj;
				addMap2Element(map2, ele2);
				ele.add(ele2);
			}else{
				ele2.setText(obj==null?"":obj.toString());
				ele.add(ele2);
			}
		}
	}
	

}

